#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient


# 连接MongoDB
def InitMongoDB():
    MONGODB_HOST = '10.10.116.30'
    MONGODB_PORT = 27017
    MONGODB_DB_NAME = 'node'
    MONGODB_COLLECTION_NAME = 'data'
    connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = connection[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return connection, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


if __name__ == "__main__":
    MongoDB_Connection, MongoDB_Collection = InitMongoDB()

    results = MongoDB_Collection.find()
    for result in results:
        id = result["_id"]
        if result.has_key("backup") and result.has_key("file_path"):
            path = result["file_path"]
            uploadtime = result["backup"]["uploadtime"]
            if path.startswith("data/"):
                new_path = "data/" + uploadtime[:4] + uploadtime[5:7] + uploadtime[8:10] + "/" + path[5:]
                print new_path
                MongoDB_Collection.update_one({"_id": id}, {"$set": {"file_path": new_path}})
    CloseMongoDB(MongoDB_Connection)
